{include file='public/header.html'}

<link href="__STATIC__/cherry/cherry-markdown.min.css" rel="stylesheet">
<link href="__STATIC__/chosen/chosen.min.css" rel="stylesheet">
<style>
    #markdown-editor {
        width: 100%;
        margin: 0 auto;
        min-height: 500px;
        border: 1px solid #ddd;
    }
</style>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar" id="sidebar">
            {include file='public/sidebar.html'}
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" id="main">
            <h3 class="page-header">修改文章</h3>

            <div class="panel panel-default">
                <div class="panel-heading">修改文章<span class="pull-right"><a href="javascript:history.back(-1);">返回</a></span></div>
                <div class="panel-body">
                    {php $category=widget('common.category')->get()}
                    <form class="form-horizontal submit-ajax" role="form" action="{:url('edit')}" method="post">
                        <input type="hidden" name="id" value="{$vo.id}" />

                        <div class="form-group">
                            <label for="title" class="col-sm-1 control-label">*标题</label>
                            <div class="col-sm-11">
                                <input type="text" id="title" name="title" class="form-control" placeholder="请输入标题" value="{$vo.title}" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label">*分类</label>
                            <div class="col-sm-11">
                                {:form_select('category_id', $category, $vo['category_id'], ' class="form-control" style="width:auto;"')}
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label">封面</label>
                            <div class="col-sm-11">
                                {php $attr=pic_attr($vo['thumb'])}
                                <input type="hidden" id="thumb" name="thumb" value="{$vo.thumb}" />
                                <div id="pic_box" class="thumb">
                                    <img id="pic_img" {$attr.img} width="90" height="60" alt="thumb" />上传封面
                                    <span id="pic_remove" {$attr.span}>删除</span>
                                    <input id="upload-input" type="file" accept="image/*" onchange="previewImg(this);">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label">*内容</label>
                            <div class="col-sm-11">
                                <div id="markdown-editor"></div>
                                <textarea id="markdown-content" name="markdown" style="display: none;">{$vo.markdown|htmlspecialchars}</textarea>
                                <textarea id="html-content" name="html" style="display: none;"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="is_auto_summary" class="col-sm-1 control-label">*自动摘要</label>
                            <div class="col-sm-11">
                                {:form_radio('is_auto_summary', '1=开启|0=关闭', $vo['is_auto_summary'])}
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="summary" class="col-sm-1 control-label">摘要</label>
                            <div class="col-sm-11">
                                <textarea name="summary" id="summary" rows="2" class="form-control" placeholder="摘要，为空则自动提取">{$vo.summary}</textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="my_tags" class="col-sm-1 control-label">*标签</label>
                            <div class="col-sm-11">
                                {php $tags=widget('common.tags')->get()}
                                {php $mytagids=explode(',', $vo['tag_ids'])}
                                <select id="my_tags" name="tag_ids[]" multiple="multiple" class="chosen-select form-control" data-placeholder="请输入或选择标签...">
                                    {foreach $tags as $tid=>$tag}
                                    <option value="{$tid}"{if in_array($tid,$mytagids):} selected{/if}>{$tag}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label">新标签</label>
                            <div class="col-sm-11">
                                <p class="form-control-static"><span id="new_tag">无新标签</span> <a href="javascript:addTag();">添加</a> <span class="text-danger">格式：标签名@标识 如：测试@test</span></p>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="url" class="col-sm-1 control-label">链接</label>
                            <div class="col-sm-11">
                                <input type="text" id="url" name="url" class="form-control" placeholder="http://开头链接" value="{$vo.url}" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="password" class="col-sm-1 control-label">密码</label>
                            <div class="col-sm-11">
                                <input type="text" id="password" name="password" class="form-control" placeholder="访问密码" value="{$vo.password}" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label">&nbsp;</label>
                            <div class="col-sm-11">
                                <input type="submit" id="submit" value="修改" class="btn btn-primary" />
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{include file='public/footer.html'}
<script>
    const GV = {
        upload_api: "{:url('api/upload')}",
        pic_upload_api: "{:url('api/upload_base64')}",
        pic_del_api: "{:url('api/image_del')}",
    }
</script>
<script src="__STATIC__/cherry/cherry-markdown.core.js"></script>
<script src="__STATIC__/js/cherry_editor.js?v={:site_ver()}"></script>
<script src="__STATIC__/js/pic_upload.js?v={:site_ver()}"></script>
<script src="__STATIC__/chosen/chosen.jquery.min.js"></script>
<script>
    $('.chosen-select').chosen({
        no_results_text: '新标签，请点击下面添加：',
        allow_single_deselect:true,
    });
    $('.chosen-select').on('chosen:no_results', function(e, params) {
        var newtag = $('#my_tags_chosen .search-field input').val();
        $('#new_tag').html(newtag);
    });
    function addTag() {
        var newtg = $('#new_tag').html();
        if (newtg !== '无新标签') {
            var url = "{:url('tags/api_add')}";
            $.post(url,{title:newtg,sort:0},function(res){
                if (res.status === 1) {
                    $("#my_tags").append('<option value="'+res.data.insert_id+'" selected>'+res.data.title+'</option>');
                    $('#my_tags').trigger("chosen:updated");
                    $('#new_tag').html('无新标签 ');
                    toast(res.msg);
                } else {
                    toast(res.msg);
                }
            },'json');

        } else {
            toast('请在上面输入新标签');
        }
    }
    //resize the chosen on window resize
    $(window).off('resize.chosen').on('resize.chosen', function() {
        $('.chosen-select').each(function() {
            var $this = $(this);
            $this.next().css({'width': $this.parent().width()});
        })
    }).trigger('resize.chosen');
    // markdown
    $(function () {
        let content = $('#markdown-content').text();
        editor.setMarkdown(content);
    });
</script>

</body>
</html>